<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;

use App\Course;
use App\Club;

class CourseController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $courses = Club::find(Auth::user()->club_id)->course;
        $temp = $courses;
        if (is_null($request->search)) {
            $courses = is_null($request->type) ? $courses : $courses->where('status', $request->type);
        } else {
            $courses = $courses->where('title', 'like', "%{$request->search}%");
        }

        return view('courses.index', [
            'courses' => $courses,
            'all' => $temp->count(),
            'status0' => $temp->where('status', 0)->count(),
            'status1' => $temp->where('status', 1)->count(),
            'status2' => $temp->where('status', 2)->count(),
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $coaches = Club::find(Auth::user()->club_id)->coach;
        return view('courses.create', compact('coaches'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //when POST request to /courses
        $request->validate([
            'title' => 'required|min:6|max:255',
            'price' => 'required|numeric|min:0|max:999',
            'section' => 'required|min:2|max:255',
            'train_time' => 'required|date|after:tomorrow'
        ]);
        Course::create([
            'title' => $request->title,
            'price' => $request->price,
            'status' => 0,
            'train_time' => Carbon::parse($request->train_time),
            'create_time' => now(),
            'section' => $request->section,
            'admin_id' => auth()->id(),
            'club_id' => Auth::user()->club_id,
            'coach_id' => $request->coaches
        ]);

        $this->flash('创建课程成功');
        return redirect('courses');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Course  $course
     * @return \Illuminate\Http\Response
     */
    public function edit(Course $course)
    {
        if ($course->club_id !== Auth::user()->club_id)
            abort('403');
        $course->coach = Club::find($course->club_id)->coach;
        $course->train_time = Carbon::parse($course->train_time);
        return view('courses.edit', compact('course'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Course  $course
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Course $course)
    {
        // update course changes
        $request->validate([
            'title' => 'required|min:6|max:255',
            'price' => 'required|numeric|min:0|max:999',
            'section' => 'required|min:2|max:255',
            'train_time' => 'required|date|after:tomorrow'
        ]);
        $course->coach_id = $request->coach_id + 0;   //string to int
        $course->save();
        $course->update(request(['title', 'price', 'train_time', 'section']));
        $this->flash('更新课程成功');
        return redirect('courses');
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Course  $course
     * @return \Illuminate\Http\Response
     */
    // 更新状态(误...)
    public function destroy(Course $course)
    {
        // Update Course Status
        $course->status == 2 ? $course->status -= 1 : $course->status += 1;
        $course->save();
        return redirect('courses');
    }

    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    // 提示信息
    function flash($message)
    {
        session()->flash('message', $message);
    }
}
